package com.handinfo.android.core.graphics;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.input.DWInputFactory;
import com.handinfo.android.core.object.IGameHandler;

public class DWScreen
{
	private DWGraphics m_graphics;
	public int m_width, m_height;
	public static DWScreen Instance;
	private IGameHandler m_gameHandler;
	private DWInputFactory m_input;
	
	private Canvas m_canvas;
	public Bitmap m_bitmap;
	private Paint m_paint;
	private Matrix m_matrix;
	
	public DWScreen(int width, int height)
	{
		m_graphics = new DWGraphics();
		m_input = new DWInputFactory();
		m_width = width;
		m_height = height;
		Instance = this;
		
		m_bitmap = Bitmap.createBitmap(DWGameManager.Screen_Width, DWGameManager.Screen_Height, Config.RGB_565);
		m_canvas = new Canvas(m_bitmap);
		m_paint = new Paint();
		m_paint.setAntiAlias(true);
		m_matrix = new Matrix();
		m_matrix.postScale((float)m_width / DWGameManager.Screen_Width, (float)m_height / DWGameManager.Screen_Height);
		m_graphics.setCanvas(m_canvas);
	}

	public IGameHandler getGameHandler()
	{
		return this.m_gameHandler;
	}

	public void setGameHandler(IGameHandler mGameHandler)
	{
		this.m_gameHandler = mGameHandler;
		m_input.setHandler(mGameHandler);
	}

	public DWInputFactory getInput()
	{
		return m_input;
	}

//	public synchronized void paint(Canvas canvas)
//	{
//		canvas.clipRect(0, 0, m_width, m_height);
//		m_graphics.setCanvas(canvas);
//		if (m_gameHandler != null) {
//			m_gameHandler.logic();
//			m_gameHandler.paint(m_graphics);
//		}
//	}
	
	public synchronized void paint(Canvas canvas)
	{
		canvas.clipRect(0, 0, m_width, m_height);
		canvas.drawBitmap(m_bitmap, m_matrix, m_paint);
	}

	public DWGraphics getGraphics()
	{
		return m_graphics;
	}

	public int getWidth()
	{
		return m_width;
	}

	public int getHeight()
	{
		return m_height;
	}
}
